﻿/*--------------------------------------------------------------------------------
 * Adaptive Intelligence - Extended Code Document Object Model
 * Version 2.10
 * 
 * AI Solution File Class
 *      Provides an XML-based storage/retrieval mechanism for writing and reading
 * AI solution files.
 *------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------
 * System Imports
 *------------------------------------------------------------------------------*/
using System;
using System.IO;
using System.Xml;
using Adaptive.Foundation;

namespace Adaptive.Foundation.CodeDom
{
    /// <summary>
    /// Provides an XML-based storage/retrieval mechanism for writing and reading
    /// AI solution files.
    /// </summary>
    public class AiSolutionFile : DisposableObjectBase
    {
        /*--------------------------------------------------------------------------------
         * Public Event Definitions
         *------------------------------------------------------------------------------*/
        #region Public Event Definitions
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Constants Declarations
         *------------------------------------------------------------------------------*/
        #region Private Constants Declarations
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Member Declarations
         *------------------------------------------------------------------------------*/
        #region Private Member Declarations
        /// <summary>
        /// Path and name of the file.
        /// </summary>
        private string _pathAndFileName = string.Empty;
        #endregion

        /*--------------------------------------------------------------------------------
         * Constructor / Dispose / Destructor Methods
         *------------------------------------------------------------------------------*/
        #region Constructor / Dispose / Destructor Methods
        /// <summary>
        /// Initializes a new instance of the class.
        /// </summary>
        /// <remarks>
        /// This is the default constructor.
        /// </remarks>
        public AiSolutionFile()
            : base()
        {
        }
        /// <summary>
        /// Initializes a new instance of the clas.
        /// </summary>
        /// <param name="pathAndFileName">
        /// The fully-qualified path and file name.
        /// </param>
        public AiSolutionFile(string pathAndFileName)
            : base()
        {
            _pathAndFileName = pathAndFileName;
        }
        /// <summary>
        /// Deallocates internally allocated objects.
        /// </summary>
        /// <param name="disposing">
        /// A value indicating whether the object is being disposed.
        /// </param>
        protected override void Dispose(bool disposing)
        {
            if ((!Disposed) && (disposing))
            {
            }
            _pathAndFileName = null;
            base.Dispose(disposing);
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Properties
         *------------------------------------------------------------------------------*/
        #region Public Properties
        /// <summary>
        /// Gets or sets the path and file name to save to or read from.
        /// </summary>
        /// <value>
        /// A fully-qualified path and file name.
        /// </value>
        public string FileName
        {
            get { return _pathAndFileName; }
            set { _pathAndFileName = value; }
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Protected Methods / Event Methods
         *------------------------------------------------------------------------------*/
        #region Protected Methods / Event Methods
        #endregion

        /*--------------------------------------------------------------------------------
         * Public Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Public Methods / Functions
        /// <summary>
        /// Clears the contents of the object instance without disposing of the instance.
        /// </summary>
        public void Clear()
        {
            _pathAndFileName = string.Empty;
        }
        /// <summary>
        /// Attempts to load the solution content from disk.
        /// </summary>
        /// <param name="pathAndFileName">
        /// The fully-qualified path and file name of the solution file.
        /// </param>
        /// <returns>
        /// An <see cref="AiSolution"/> instance if successful; otherwise, returns <b>null</b>.
        /// </returns>
        public AiSolution LoadSolution(string pathAndFileName)
        {
            _pathAndFileName = pathAndFileName;
            return LoadSolution();
        }
        /// <summary>
        /// Attempts to load the solution content from disk.
        /// </summary>
        /// <returns>
        /// An <see cref="AiSolution"/> instance if successful; otherwise, returns <b>null</b>.
        /// </returns>
        public AiSolution LoadSolution()
        {
            FileStream inputfile = null;            //Inout file.
            StreamReader textReader = null;         //Text reader instance.
            XmlTextReader reader = null;            //Xml reader instance.
            AiSolution newSolution = null;          //Return value.
            bool result = false;                    //Load result.

            //Validate parameters.
            if (File.Exists(_pathAndFileName))
            {
                try
                {
                    inputfile = new FileStream(_pathAndFileName, FileMode.Open, FileAccess.Read);
                }
                catch
                {
                    inputfile = null;
                }

                if (inputfile != null)
                {
                    textReader = new StreamReader(inputfile);
                    reader = new XmlTextReader(textReader);

                    newSolution = new AiSolution();
                    result = newSolution.Load(reader);
                    if (!result)
                    {
                        newSolution.Dispose();
                        newSolution = null;
                    }

                    reader.Close();
                    textReader.Close();
                    textReader.Dispose();
                    inputfile.Close();
                    inputfile.Dispose();
                }
            }
            return newSolution;
        }
        /// <summary>
        /// Attempts to save the solution content to disk.
        /// </summary>
        /// <param name="solution">
        /// The <see cref="AiSolution"/> instance to be saved.
        /// </param>
        /// <param name="pathAndFileName">
        /// The fully-qualified path and file name to save to.
        /// </param>
        /// <returns>
        /// <b>true</b> if the operation is successful; otherwise, returns <b>false</b>.
        /// </returns>
        public bool SaveSolution(AiSolution solution, string pathAndFileName)
        {
            _pathAndFileName = pathAndFileName;
            return SaveSolution(solution);
        }
        /// <summary>
        /// Attempts to save the solution content to disk.
        /// </summary>
        /// <param name="solution">
        /// The <see cref="AiSolution"/> instance to be saved.
        /// </param>
        /// <returns>
        /// <b>true</b> if the operation is successful; otherwise, returns <b>false</b>.
        /// </returns>
        public bool SaveSolution(AiSolution solution)
        {
            FileStream outputfile = null;           //Output file.
            StreamWriter textWriter = null;         //Text writer instance.
            XmlTextWriter writer = null;            //Xml writer instance.
            bool returnValue = false;               //Return value.

            //Validate parameters.
            if(solution == null)
                throw new ArgumentNullException("solution");

            if (File.Exists(_pathAndFileName))
                File.Delete(_pathAndFileName);

            try
            {
                outputfile = new FileStream(_pathAndFileName, FileMode.CreateNew, FileAccess.Write);
            }
            catch
            {
                outputfile = null;
            }

            if (outputfile != null)
            {
                textWriter = new StreamWriter(outputfile);
                writer = new XmlTextWriter(textWriter);

                returnValue = solution.Save(writer);

                writer.Flush();
                writer.Close();
                textWriter.Close();
                textWriter.Dispose();
                outputfile.Close();
                outputfile.Dispose();
            }

            return returnValue;
        }
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Event Handlers
         *------------------------------------------------------------------------------*/
        #region Private Event Handlers
        #endregion

        /*--------------------------------------------------------------------------------
         * Private Methods / Functions
         *------------------------------------------------------------------------------*/
        #region Private Methods / Functions
        #endregion

    }
}
